﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Fields;
using Microsoft.SharePoint.Publishing.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.SharePoint.WebControls;

namespace GetSharePointToValidate.Adapters
{
    public class GSPTV_SummaryLinkFieldWebPart : ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.Page is Microsoft.SharePoint.Publishing.PublishingLayoutPage && SPContext.Current.FormContext.FormMode.Equals(SPControlMode.Display))
            {
                SummaryLinkWebPart SummaryLinkFieldControl = this.Control as SummaryLinkWebPart;
                SummaryLinkCollection summaryLinkCollection = SummaryLinkFieldControl.SummaryLinkValue.SummaryLinks;
                if (summaryLinkCollection.Count > 0)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                    for (int i = 0; i < summaryLinkCollection.Count; i++)
                    {
                        SummaryLink summaryLink = summaryLinkCollection[i] as SummaryLink;
                        HyperLink hyperLink = new HyperLink();
                        hyperLink.NavigateUrl = summaryLink.LinkUrl;
                        hyperLink.Text = summaryLink.Title;
                        hyperLink.ToolTip = summaryLink.Description;

                        if (summaryLink.OpenInNewWindow)
                            hyperLink.Attributes.Add("rel", "external");

                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                        hyperLink.RenderControl(writer);

                        if (summaryLink.Description.Length > 0)
                        {
                            writer.RenderBeginTag(HtmlTextWriterTag.P);
                            writer.WriteLine(summaryLink.Description);
                            writer.RenderEndTag();
                        }

                        writer.RenderEndTag();
                    }
                    writer.RenderEndTag();
                }
            }
            else
                base.Render(writer);
        }
    }
}
